package core.manager.battle
{
	import values.avatar.IAvatarPropertyPluser;
	import values.avatar.IAvatarValue;
	import values.avatar.IPropertyProvider;
	import values.common.IAvatarProperty;
	import values.skill.LogicEffectValue;
	import values.skill.SkillValue;

	public class BattleCountManager
	{
		/**
		 * 等级优势基数 
		 */		
		public static const LEVEL_WEAKEN:Number = -3;
		
		/**
		 * 普通攻击造成的伤害 
		 * @param A
		 * @param B
		 * @return 
		 * 
		 */		
		public static function countNormalDamage(A:IAvatarValue , B:IAvatarValue):Object
		{
			var normalHit:Number = countNormalHit(A, B);
			var isSuppressRate:Boolean = countRandom(countSuppressRate(A,B));
			var returnNum:Number = normalHit;
			var state:Array = [];
			if(isSuppressRate)
			{
				returnNum += normalHit * Math.min(Math.max( ( A as IPropertyProvider).suppressEffect , 25) , 100) / 100;
				state.push("suppress");
			}
			else
			{
				if(countRandom(countDodgeRate(A , B)))
				{
					return  {data:0 , state:["dodge"]};
				}
			}
			
			if(countRandom(countCritRate(A , B)))
			{
				returnNum += normalHit * Math.max( Math.min(300 ,( A as IPropertyProvider ).critEffect) , 50) / 100;
				state.push("crit");
			}
			
			if(countRandom(countBlockRate(A , B)))
			{
				returnNum -= normalHit * Math.min( Math.max(25 , ( B as IPropertyProvider ).blockEffect) , 67 );
				state.push("block");
			}
			
			return {data:Math.max(A.level * 3 , returnNum) , state:state};
		}
		
		/**
		 * 技能造成的基本伤害 
		 * @param A
		 * @param B
		 * @param skill
		 * @return 
		 * 
		 */		
		public static function countSkillBaseDamage(A:IAvatarValue , B:IAvatarValue , skill:SkillValue):Object
		{
			var normalHit:Number = countSkillHit(A , B);
			
			
			var returnNum:Number = normalHit;
			
			var state:Array = [];
			if( skill.checkSkillHasLogic(LogicEffectValue.MUST_SUPPRESS) || countRandom(countSuppressRate(A,B)))
			{
				returnNum += normalHit * Math.min(Math.max( ( A as IPropertyProvider ).suppressEffect , 25) , 100) / 100;
				state.push( "suppress" );
			}
			else
			{
				if(!skill.checkSkillHasLogic(LogicEffectValue.IGNORE_DODGE) && countRandom(countDodgeRate(A , B)))
				{
					return  {data:0 , state:["dodge"]};
				}
			}
			
			if(countRandom(countCritRate(A , B)))
			{
				returnNum += normalHit * Math.max( Math.min(300 , ( A as IPropertyProvider ).critEffect) , 50) / 100;
				state.push("crit");
			}
			
			if(!skill.checkSkillHasLogic(LogicEffectValue.IGNORE_BLOCK) && countRandom(countBlockRate(A , B)))
			{
				returnNum -= normalHit * Math.min( Math.max(25 ,( B as IPropertyProvider).blockEffect) , 67 );
				state.push("block");
			}
			
			return {data:Math.max(A.level * 6 , returnNum) , state:state};
		}
		
		/**
		 * 普通伤害 
		 * @param A
		 * @param B
		 * @param levelWeaken
		 * @return 
		 * 
		 */		
		private static function countNormalHit(A:IAvatarValue , B:IAvatarValue , levelWeaken:Object = null):Number
		{
			if(A.avatarType.mainProperty == "normalPower")
			{
				return (A as IPropertyProvider).normalDamage - (B as IPropertyProvider).normalDefense;
			}
			else
			{
				return (A as IPropertyProvider).elementDamage - (B as IPropertyProvider).elementDefense;
			}
		}
		
		/**
		 * 技能伤害 
		 * @param A
		 * @param B
		 * @param levelWeaken
		 * @return 
		 * 
		 */		
		private static function countSkillHit(A:IAvatarValue , B:IAvatarValue , levelWeaken:Object = null):Number
		{
			if(A.avatarType.mainProperty == "normalPower")
			{
				return (A as IPropertyProvider).normalDamage + (A as IPropertyProvider).skillDamage - (B as IPropertyProvider).skillDefense;
			}
			else
			{
				return (A as IPropertyProvider).elementDamage + (A as IPropertyProvider).skillDamage - (B as IPropertyProvider).skillDefense;
			}
		}
			
		/**
		 * 计算压制等级 
		 * @param A
		 * @param B
		 * @param levelWeaken
		 * @return 
		 * 
		 */		
		private static function countSuppressRate(A:IAvatarValue , B:IAvatarValue , levelWeaken:Object = null):Number
		{
			if(A.level < 30) return -1;
			return  -3 + A.suppressRateBonus - B.defenseRateBonus + ((A as IPropertyProvider).suppressRate - (B as IPropertyProvider).defenseRate) * 6 / ( 10 * A.level );
		}
		
		/**
		 * 计算闪避等级 
		 * @param A
		 * @param B
		 * @param levelWeaken
		 * @return 
		 * 
		 */		
		private static function countDodgeRate(A:IAvatarValue , B:IAvatarValue , levelWeaken:Object = null):Number
		{
			return Math.min(75 , 5 + B.dodgeRateBonus - A.hitRateBonus + ((B as IPropertyProvider).dodgeRate - (A as IPropertyProvider).hitRate) * 275  / (100 * B.level) );
		}
		
		/**
		 * 计算抵抗等级 
		 * @param A
		 * @param B
		 * @param leavelWeaken
		 * @return 
		 * 
		 */		
		public static function countResistRate(A:IAvatarValue , B:IAvatarValue , leavelWeaken:Object = null):Number
		{
			return Math.min(95 , 40 + B.resistRateBonus - A.penetrateBonus + ((B as IPropertyProvider).resistRate - (A as IPropertyProvider).penetrateRate) * 175 / ( 100 * B.level));
		}
		
		/**
		 * 计算格挡 
		 * @param A
		 * @param B
		 * @param leavelWeaken
		 * @return 
		 * 
		 */		
		private static function countBlockRate(A:IAvatarValue , B:IAvatarValue , leavelWeaken:Object = null):Number
		{
			return Math.min(95 , B.blockRateBonus - A.enforceRateBonus + ( (B as IPropertyProvider).blockRate - (A as IPropertyProvider).enforceRate ) * 75 / ( 100 * B.level ));
		}
		
		/**
		 * 计算暴击 
		 * @param A
		 * @param B
		 * @param leavelWeaken
		 * 
		 */		
		private static function countCritRate(A:IAvatarValue , B:IAvatarValue , leavelWeaken:Object = null):Number
		{
			return Math.min(85 , Math.max(5 , A.critRateBonus - B.toughnessRateBonus + ((A as IPropertyProvider).critRate - (B as IPropertyProvider).toughness) * 15 / (10 * A.level)));
		}
		
		/**
		 * 计算几率 
		 * @param value
		 * @return 
		 * 
		 */		
		public static function countRandom(value:Number):Boolean
		{
			var seed:Number = Math.random() * 100;
			
			if(seed <= value)
			{
				return true;
			}
			
			return false;
		}
		
//		private static function countLevelWeaken(A:IAvatarValue , B:IAvatarValue):Object
//		{
//			var level_num:Number = A.level - B.level;
//			
//			if(level_num > 2)
//			{
//				level_num = Math.max(( level_num - 2 ) * LEVEL_WEAKEN , -50);
//				
//				return {who:B , data:level_num};
//			}
//			else 
//			{
//				if(level_num < -2)
//				{
//					level_num = Math.max((level_num + 2) * -1 * LEVEL_WEAKEN  , -50);
//					
//					return {who:A , data:level_num};
//				}
//			}
//			
//			return null;
//		}
		
		
		
		public static function createBattleData(A:* , B:* , battlefield:*):*
		{
			
		}
	}
}